package com.zkyc.framework.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.utils.FFMPEG;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.website.UploadVo;
import com.zkyc.framework.file.service.MinIoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.zkyc.framework.common.utils.FFMPEG.downloadPathFile;

@Slf4j
@RestController
@RequestMapping
public class FileController {

    @Value("${zjlab.logo}")
    private String logo;

    @Value("${zjlab.savePath}")
    private String savePath;

    @Autowired
    private MinIoService minIoService;


    /**
     * 文件上传到minio服务器
     */
    @PostMapping("upload")
    public UploadVo upload(HttpServletRequest request, String belongKey) throws Exception {
        JSONObject upload = null;
        try {
            log.info("请求进入接口");
            upload = minIoService.upload(request, belongKey);
        } catch (Exception e) {
            e.printStackTrace();
            return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), e.toString(), e.getMessage(), 1);
        }
        if (upload != null) {
            return UploadVo.insertData(ResultCode.SUCCESS.getCode(), upload, "上传成功", 0);
        }
        return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), null, "上传失败", 1);
    }

    /**
     * 文件上传到minio服务器
     */
    @GetMapping("uploadByUrl")
    public UploadVo uploadByUrl(String url, String websiteId) throws Exception {
        JSONObject upload = null;
        try {
            log.info("请求进入接口");
            upload = minIoService.uploadByUrl(url, websiteId);
        } catch (Exception e) {
            e.printStackTrace();
            return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), e.toString(), "上传失败", 1);
        }
        if (upload != null) {
            return UploadVo.insertData(ResultCode.SUCCESS.getCode(), upload, "上传成功", 0);
        }
        return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), null, "上传失败", 1);
    }

    /**
     * minio下载文件
     */
    //未使用该接口
    @PostMapping("downloadWitnWaterMark")
    public void download(String url, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] split = url.split("/");
        String fileName = split[split.length - 1];
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        HashMap<String, String> dto = new HashMap<String, String>();
        dto.put("input_path", url);// 必填
        dto.put("video_converted_path", savePath + "/" + fileName);// 必填
        dto.put("logo", logo);
        String secondsString = new FFMPEG().videoTransfer(dto);
        System.out.println("转换共用:" + secondsString + "秒");
        downloadPathFile(savePath + "/" + fileName, request, response);
    }

    /**
     * 文件下载
     *
     * @param response
     */
    //未使用该接口
    @PostMapping("download")
    public ResultVO download(HttpServletResponse response, @RequestBody Map<String, String> map) {
        System.out.println(map);
        String url = map.get("url");
        if (url != null) {
            minIoService.download(response, url);
            return ResultVO.success(ResultCode.SUCCESS);
        }
        return ResultVO.fail(ResultCode.FAILURE);
    }

    /**
     * 文件上传到minio服务器
     */
    @PostMapping("commonUpload")
    public UploadVo commonupload(HttpServletRequest request) {
        JSONObject upload = null;
        try {
            log.info("请求进入接口");
            upload = minIoService.commonUpload(request);
        } catch (Exception e) {
            e.printStackTrace();
            return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), e.toString(), "上传失败", 1);
        }
        if (upload != null) {
            return UploadVo.insertData(ResultCode.SUCCESS.getCode(), upload, "上传成功", 0);
        }
        return UploadVo.insertData(ResultCode.UPLOAD_FAILED.getCode(), null, "上传失败", 1);
    }

}
